package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.yun.YunAction;

import org.json.JSONObject;

import longcaisuyun.longcai.com.net.PostDingData;
import longcaisuyun.longcai.com.net.PostQiangDan;
import longcaisuyun.longcai.com.utils.MyTimeOut;


//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//

/************************************************************/

public class GuangBoActivity extends Activity {
    public static final String action = "longcaisuyun.com.longcaisuyun.dele";
    TextView textView56;
    TextView textView57;
    String orderid = "";
    TextView textView59;
    TextView textView61;
    TextView textView64;
    TextView textView65;
    TextView textView66,tv_pay,tv_hulv,tv_saycontent;
    Button button5;
    Button button6;
    RelativeLayout R_back;
    private Dialog dialog;//提示dialog
    private MyTimeOut myTimeOut;//倒计时
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guang_bo);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        orderid = getIntent().getStringExtra("orderid");
        Log.e("orderid", orderid);
        ViewInit();
        TiShi();
        DataInit();
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.myviewutil.startProgressDialog(GuangBoActivity.this);
                new PostQiangDan(MyApplication.myPreferences.readUid(), orderid, new AsyCallBack<PostQiangDan.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, PostQiangDan.Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        if (info.message.equals("1")) {
                            Toast.makeText(GuangBoActivity.this, "抢单成功，请耐心等待抢单结果", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(action);
                            intent.putExtra("data", orderid);
                            sendBroadcast(intent);
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("type", "1");
                            YunAction.getInstance().sendMessage(GuangBoActivity.this, getIntent().getStringExtra("uid"), jsonObject.toString());
                        } else if (info.message.equals("2")) {
                            Toast.makeText(GuangBoActivity.this, "司机运货中，不能抢单", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(GuangBoActivity.this, "抢单失败", Toast.LENGTH_SHORT).show();
                        }
                        finish();
                        MyApplication.myviewutil.stopProgressDialog();
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                        Toast.makeText(GuangBoActivity.this, "网络获取数据失败", Toast.LENGTH_SHORT).show();
                    }
                }).execute(GuangBoActivity.this);
            }
        });
        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(action);
                intent.putExtra("data", orderid);
                sendBroadcast(intent);
                finish();
            }
        });

    }

    private void TiShi() {
//        Log.e("是否提示", String.valueOf(MyApplication.myPreferences.readTiShi()));
        if (MyApplication.myPreferences.readTiShi()){
            dialog=new Dialog(GuangBoActivity.this,R.style.FullDialog);
            dialog.show();
            dialog.setCancelable(true);
            View view=LayoutInflater.from(GuangBoActivity.this).inflate(R.layout.dialog_tishi,null);
            MyApplication.ScaleScreenHelper.loadView((ViewGroup)view);
            dialog.setContentView(view);
            tv_hulv= (TextView) dialog.findViewById(R.id.tv_hulv);
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    myTimeOut.start();
                }
            });
            tv_hulv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyApplication.myPreferences.saveTiShi(false);
                    dialog.dismiss();
                    myTimeOut.start();
                }
            });
        }else{
            myTimeOut.start();
        }
    }

    private void DataInit() {
        MyApplication.myviewutil.startProgressDialog(GuangBoActivity.this);
        new PostDingData(MyApplication.myPreferences.readUid(), orderid, new AsyCallBack<PostDingData.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostDingData.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("1")) {
                    MyApplication.myviewutil.stopProgressDialog();
                    if (info.ordertype.equals("1")) {
                        textView57.setText("实时订单");
                        R_back.setBackgroundColor(Color.parseColor("#d7696f"));
                        textView57.setBackgroundResource(R.drawable.corner_guangbo);
//                        String[] str=info.listDiZhi.get(0).split(",");
//                        String[] loc = MyApplication.myPreferences.readProvinceId().split(",");
//                        Double km= JuLiJiSuan.getDistance(Double.valueOf(str[0]), Double.valueOf(str[1]), Double.valueOf(loc[0]), Double.valueOf(loc[1]));
                        textView56.setText("距您" + info.range+"" + "KM");
                        if (info.payttime.equals("1")){
                            tv_pay.setText("发货时付款");
                        }else{
                            tv_pay.setText("收货时付款");
                        }
                        textView65.setVisibility(View.GONE);
                    } else {
                        textView56.setText(info.usetime+"   " +info.week+ "   " + info.time);
                        textView57.setText("预约订单");
                        textView57.setBackgroundResource(R.mipmap.guangbo_kind);
                        tv_pay.setTextColor(Color.parseColor("#ffffff"));
                        tv_pay.setVisibility(View.GONE);
                        if (info.payttime.equals("1")){
//                            tv_pay.setText("发货时付款");
                            textView65.setText("发货时付款");
                        }else{
//                            tv_pay.setText("收货时付款");
                            textView65.setText("收货时付款");
                        }
                    }
                    textView59.setText(info.list.get(0));
                    textView61.setText(info.list.get(info.list.size() - 1));
                    textView64.setText(info.totalmoney + "元+"+info.tip+ "元");
                    tv_saycontent.setText(info.saycontent);
//                    if (info.account.length() > 0) {

//                    }
//                    if (info.reward.length() > 0) {
//                        textView66.setText("+" + info.tip+ "元)");
//                    }
                } else {
                    Toast.makeText(GuangBoActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                    MyApplication.myviewutil.stopProgressDialog();
                }
            }
            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
                MyApplication.myviewutil.stopProgressDialog();
                Intent intent = new Intent(action);
                intent.putExtra("data", orderid);
                sendBroadcast(intent);
                finish();
                Toast.makeText(GuangBoActivity.this, "订单已被别人抢走或被取消", Toast.LENGTH_SHORT).show();
            }
        }).execute(GuangBoActivity.this);
    }

    private void ViewInit() {
        R_back = (RelativeLayout) findViewById(R.id.R_back);
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        textView56 = (TextView) findViewById(R.id.textView56);
        textView57 = (TextView) findViewById(R.id.textView57);
        textView59 = (TextView) findViewById(R.id.textView59);
        textView61 = (TextView) findViewById(R.id.textView61);
        textView64 = (TextView) findViewById(R.id.textView64);
        textView65 = (TextView) findViewById(R.id.textView65);
        textView66 = (TextView) findViewById(R.id.textView66);
        tv_saycontent= (TextView) findViewById(R.id.tv_saycontent);
        tv_pay=(TextView) findViewById(R.id.tv_pay);
        button5 = (Button) findViewById(R.id.button5);
        button6 = (Button) findViewById(R.id.button6);
        myTimeOut=new MyTimeOut(11000,1000,button5);
    }
}
